import time
import requests
import re
import numpy as np
import pandas as pd
# 同一地区不同岗位收入差异
# 不同地区同一岗位收入差异
# 工作经验和薪资的关系
# 地区的平均薪资水平
# 同一城市不同区的薪资差异
# 不同学历薪资差异
# 不同薪资段对于要掌握的技术的关系
# 某一项专业技能和薪资的关系、
# 我国各地区工资分布
# 不同规模公司的薪资水平
# 不同规模公司对岗位的需求
# https://www.51job.com/


import time
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Firefox(executable_path=r'D:\bianchengerbianchenger\driver\geckodriver.exe')
# driver = webdriver.Chrome(executable_path='D:/11111/chromedriver.exe')
driver.get('https://www.zhaopin.com/')
driver.maximize_window()
# driver.maximize_window()

matrix = np.zeros((10000,13))

def get_message_of(content_you_search,page):
    # 登陆
    driver.find_element(By.XPATH, '//*[@id="zpPassportWidget"]/div/div/div/div/div[1]/div/div').click()
    time.sleep(8)
    # 搜索
    driver.find_element(By.CLASS_NAME, 'a-input__native').send_keys(content_you_search)
    time.sleep(1)
    driver.find_element(By.XPATH, '//*[@id="rightNav_top"]/div/div[2]/div/div/div[2]/button').click()
    time.sleep(2)
    # 操作第二个界面
    cls = driver.window_handles
    time.sleep(1)
    driver.switch_to.window(cls[1])
    time.sleep(1)



    final_job_name = list()
    final_sal = list()
    final_min_sal = list()
    final_max_sal = list()
    final_ac = list()
    final_city = list()
    final_area = list()
    final_location = list()
    final_year = list()
    final_education = list()
    final_com = list()
    final_dc_at = list()
    final_renshu = list()
    final_date = list()

    for i in range(page):
        time.sleep(3)
        print('----------------------'+str(i)+'----------------------------------')
        time.sleep(1)
        all_div = driver.find_element(By.CSS_SELECTOR, 'div.sou-main__list').find_elements(By.CSS_SELECTOR,'div.joblist-box__item.clearfix')
        for i in all_div:
            job_name = i.find_element(By.CSS_SELECTOR, 'span.iteminfo__line1__jobname__name').text
            # 薪资
            sal = i.find_element(By.CSS_SELECTOR, 'p.iteminfo__line2__jobdesc__salary').text
            min_sal = None
            max_sal = None
            if sal[-1] == '万':
                if '千' in sal.split('-')[0]:
                    min_sal = float(sal.split('-')[0][:-1]) * 1000
                    max_sal = float(sal.split('-')[1][:-1]) * 10000
                else:
                    min_sal = float(sal.split('-')[0][:1]) * 10000
                    max_sal = float(sal.split('-')[1][:1]) * 10000
            elif sal[-1] == '千':
                min_sal = float(sal.split('-')[0][:1]) * 1000
                max_sal = float(sal.split('-')[1][:-1]) * 1000
            elif sal[-1] == '薪':
                # sal = '6千-1万 · 13薪'
                if sal[-7] == '万':
                    if '千' in sal.split('-')[0]:
                        min_sal = float(sal.split('-')[0][:-1]) * 1000
                        max_sal = float(sal.split('-')[1][:1]) * 10000
                    else:
                        min_sal = float(sal.split('-')[0][:1]) * 1000
                        max_sal = float(sal.split('-')[1][:1]) * 10000
                elif sal[-7] == '千':
                    min_sal = float(sal.split('-')[0][:1]) * 1000
                    max_sal = float(sal.split('-')[1][:1]) * 1000
                xin = sal.split(' · ')[1][:2]
                xin = float(xin)
                min_sal = min_sal * xin / 12
                max_sal = max_sal * xin / 12
                min_sal = round(min_sal, 2)
                max_sal = round(max_sal, 2)

            ac = i.find_element(By.CSS_SELECTOR, 'ul.iteminfo__line2__jobdesc__demand')   #重新定位
            # 工作地区
            location = ac.find_element(By.CSS_SELECTOR, 'li').text
            if '-' in location:
                city = location.split('-')[0]
                area = location.split('-')[1]
            else:
                area = None
            # 工作年限
            work_type = ac.find_element(By.CSS_SELECTOR, 'li+li').text
            year = None
            if work_type == '不限' or '无经历':
                year = 0
            else:
                year = int(work_type[0])
            # 教育背景
            try:
                education = ac.find_element(By.CSS_SELECTOR, 'li+li+li').text
            except Exception:
                education = '无学历'
            # 公司名称
            com = i.find_element(By.CSS_SELECTOR, 'span.iteminfo__line1__compname__name').text
            # 重新定位
            ab = i.find_element(By.CSS_SELECTOR, 'div.iteminfo__line2__compdesc')   #重新定位
            # 公司性质
            try:
                dc_at = ab.find_element(By.CSS_SELECTOR, 'span').text
            except Exception:
                dc_at = None
            # 公司人数
            try:
                renshu = ab.find_element(By.CSS_SELECTOR, 'span+span').text
            except Exception:
                renshu = None
            # 发布日期
            date = i.find_element(By.CSS_SELECTOR, 'span.iteminfo__line3__status__recruit').text
            print(job_name, min_sal,max_sal, city, area, year, education, com, dc_at, renshu, date)


            final_job_name.append(job_name)
            final_sal.append(sal)
            final_min_sal.append(min_sal)
            final_max_sal.append(max_sal)
            final_location.append(location)
            final_city.append(city)
            final_area.append(area)
            final_year.append(year)
            final_education.append(education)
            final_com.append(com)
            final_dc_at.append(dc_at)
            final_renshu.append(renshu)
            final_date.append(date)

            matrix=np.vstack((final_job_name,final_min_sal,final_max_sal,final_city,final_area,final_year,final_education,final_com
                              ,final_dc_at,final_renshu,final_date))

        time.sleep(3)
        ck = driver.find_element(By.XPATH, '//*[@id="positionList-hook"]/div/div[31]/div[2]/div/button[2]')
        driver.execute_script("arguments[0].click();", ck)

    return matrix

list1 = get_message_of('软件测试',2)
print(list1)
print(type(list1))
list2 = pd.DataFrame(list1)
print(list2)
list3 = pd.DataFrame(list2.values.T)
list3.to_csv('data.csv')

